// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import store from './store'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-default/index.css'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import 'normalize.css/normalize.css'
import '@/assets/iconfont/iconfont'
import IconSvg from '@/components/Icon-svg/index.vue'
import { getToken } from '@/utils/auth'
import forgetPwd from '@/components/ForgetPassword/index.vue';
import 'vue-awesome/icons'
import Icon from 'vue-awesome/components/Icon'
import axios from '@/utils/fetch';
import dict from '@/utils/dictionary';
//网址配置
import serverConfig from '@/api/serverConfig';
Vue.config.productionTip = false
//网址配置
Vue.use(serverConfig);

Vue.use(ElementUI);
Vue.use(dict);
Vue.component('icon-svg', IconSvg);
Vue.component('icon', Icon);
Vue.component('forget-pwd', forgetPwd);
Vue.prototype.$http = axios;
const whiteList = ['/login'];
router.beforeEach((to, from, next) => {
	NProgress.start();
	if (getToken()) {
		if (to.path === '/login') {
			next({
				path: '/'
			});
		} else {
			if (store.getters.roles.length === 0) {
				store.dispatch('GetInfo').then(res => {
				});
				store.dispatch('GetMenu').then(res => {
					let menus = res.data;
					let account = store.getters.account;
					store.dispatch('GenerateRoutes', {
						menus,
						account
					}).then(() => {
						router.addRoutes(store.getters.addRouters);
						next({
							...to
						});
					})
				});
			} else {
				next();
			}
		}
	} else {
		if (whiteList.indexOf(to.path) !== -1) {
			next()
		} else {
			next('/login');
			NProgress.done();
		}
	}
});

router.afterEach(() => {
	NProgress.done();
});
new Vue({
	el: '#app',
	router,
	store,
	template: '<App/>',
	components: {
		App
	}
})